import java.io.File;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;


public class LogUtil
{
    private static LogUtil instance = new LogUtil();
    
    private LogUtil()
    {
        
    }
    
    public static LogUtil getInstance()
    {
        return instance;
    }
    
    public void writeLog(String content)
    {
        try
        {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            DateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
            String logTime = dateFormat.format(new Date());
            File logDir = new File("log");
            if (!logDir.exists())
            {
                logDir.mkdir();
            }
            FileWriter writer = new FileWriter(".\\log" + File.separator + dayFormat.format(new Date()) + ".log", true);
            System.out.println(content);
            writer.write(logTime + ":" + getMS() + "---" + content + "\n");
            writer.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private String getMS()
    {
        long ms = new GregorianCalendar().getTimeInMillis()%1000;
        String msStr = String.valueOf(ms);
        for (int i = msStr.length(); i < 3; i++)
        {
            msStr = "0" + msStr;
        }
        return msStr;
    }
}
